package gclient;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class CasillasGUI extends javax.swing.JPanel implements MouseListener {
    private TableroGUI tablero;
    private ImageIcon fondo;
    private static int [] casillaMarcada = new int[2];
    public int a=0;
    public CasillasGUI() {        
        // este constructor no se usar�, se deja para poder crear el bean.        
    }
    public CasillasGUI(TableroGUI t) {
        initComponents();        
        this.tablero = t;
        if(this.tablero.getTipoTablero() == true){// tablero responde a clics?
            this.addMouseListener(this);
        }
    }
    public void setFondo(ImageIcon fondo){
        this.fondo = fondo;
    }
    public ImageIcon getFondo(){        
        return this.fondo;
    }
    private void initComponents() {
        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 161, Short.MAX_VALUE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 193, Short.MAX_VALUE)
        );
    }                       
    public void paintComponent(Graphics g){
        super.paintComponent(g);
        g.drawImage(fondo.getImage(), 0,0,this.getWidth(),this.getHeight(),this);
    }
    public void mouseClicked(MouseEvent e){}
    public void mouseEntered(MouseEvent e){}
    public void mouseExited(MouseEvent e){}
    public void mousePressed(MouseEvent e){
    	if(a == 0){
    		if(e.isMetaDown()){}
    		if(e.isAltDown()){
    			this.setCasillaMarcada(tablero.getCoordenadas((CasillasGUI)e.getComponent())); 
                this.tablero.despintar(this.getCasillaMarcada()[0],this.getCasillaMarcada()[1]);
                System.out.println("Bomba Fallida");
                a = 0;
    		}
    		else {
    			this.setCasillaMarcada(tablero.getCoordenadas((CasillasGUI)e.getComponent())); 
                this.tablero.pintar(this.getCasillaMarcada()[0],this.getCasillaMarcada()[1]);
                System.out.println("Marcado");
                if(e.isPopupTrigger()){a=1;}
    		}
    	}
    	if(a == 1) {
    		if(e.isAltDown()){
			  this.setCasillaMarcada(tablero.getCoordenadas((CasillasGUI)e.getComponent())); 
              this.tablero.despintar(this.getCasillaMarcada()[0],this.getCasillaMarcada()[1]);
              System.out.println("Bomba Utilizada");
              a = 0;
              }
    		if(e.isMetaDown()){
    			System.out.println("Casilla ya marcada");
    		}
    		else{System.out.println("Casilla ya marcada");}
    	}
    }
    public void mouseReleased(MouseEvent e){}
    public static int[] getCasillaMarcada() {
        return casillaMarcada;
    }
    public static void setCasillaMarcada(int[] aCasillaMarcada) {
        casillaMarcada = aCasillaMarcada;
    }                  
}